package com.sskj.functions.ui.fragment;


import android.annotation.SuppressLint;
import android.arch.lifecycle.Observer;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.sskj.common.base.App;
import com.sskj.common.box.imageloader.GlideImageLoader;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.RxSchedulersHelper;
import com.sskj.common.util.SPUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.functions.R;
import com.sskj.functions.R2;
import com.sskj.functions.component.DaggerUserDataComponent;
import com.sskj.functions.presenter.FunctionsPresenter;
import com.sskj.functions.view.ZoomOutPageTransformer;
import com.sskj.lib.BaseHttpConfig;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.SPConfig;
import com.sskj.lib.SpApplication;
import com.sskj.lib.base.BaseFragment;
import com.sskj.lib.bean.CertificationBean;
import com.sskj.lib.bean.FunctionsBannerBean;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import io.reactivex.Observable;

/**
 * @author Hey
 * Create at  2019/06/24
 */
@Route(path = RConfig.FUNCTIONS_FRAGMENT_FUNCTIONS)
public class FunctionsFragment extends BaseFragment<FunctionsPresenter> {

    @BindView(R2.id.functions_huoqi_ll)
    LinearLayout llHuoQiBao;
    @BindView(R2.id.functions_jbsx_ll)
    LinearLayout llJieBi;
    @BindView(R2.id.functions_dyjb_ll)
    LinearLayout llDiYa;
    @BindView(R2.id.functions_cltz_ll)
    LinearLayout llCeLue;
    @BindView(R2.id.functions_shandui_ll)
    LinearLayout llShanDui;
    @BindView(R2.id.functions_dingtou_ll)
    LinearLayout llDingTou;
    @BindView(R2.id.functions_paodekuai_ll)
    LinearLayout llPaodeKuai;
    @BindView(R2.id.functions_dzp_ll)
    LinearLayout llZhuanPan;
    @BindView(R2.id.functions_phone_iv)
    ImageView functionsPhoneIv;
    @BindView(R2.id.functions_swhz_rl)
    RelativeLayout functionsSwhzRl;
    @BindView(R2.id.banner)
    Banner banner;
    @Inject
    UserViewModel userViewModel;
    private UserData userData;

    @Override
    public int getLayoutId() {
        return R.layout.functions_fragment_functions;
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            mPresenter.getBanner();
        }
    }

    @Override
    public FunctionsPresenter getPresenter() {
        return new FunctionsPresenter();
    }

    @Override
    public void onResume() {
        super.onResume();
        mPresenter.getBanner();
    }

    @Override
    public void initView() {
        DaggerUserDataComponent.create().inject(this);
        banner.setImageLoader(new GlideImageLoader());
        banner.setPageTransformer(true, new ZoomOutPageTransformer());
        setClick();
    }


    @Override
    public void initData() {
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
            } else {
                userData = null;
            }
        });
    }

    private void setClick() {
        /*活期宝*/
        ClickUtil.click(llHuoQiBao, () -> {
            if (checkCetification()) {
                ARouter.getInstance().build(RConfig.CURRENT_TREASURE).navigation();
            }
        });

        /*借币生息*/
        ClickUtil.click(llJieBi, () -> {
            if (checkCetification()) {
                ARouter.getInstance().build(RConfig.FUNCTIONS_JBSX).navigation();
            }
        });

        /*抵押借币*/
        ClickUtil.click(llDiYa, () -> {
            if (checkCetification()) {
                ARouter.getInstance().build(RConfig.FUNCTIONS_DYJB).navigation();
            }
        });

        /*闪兑*/
        ClickUtil.click(llShanDui, () -> {
            if (checkCetification()) {
                ARouter.getInstance().build(RConfig.FUNCTIONS_FRASH).navigation();
            }
        });

        /*策略投资*/
        ClickUtil.click(llCeLue, () -> {
            if (checkCetification()) {
                ARouter.getInstance().build(RConfig.FUNCTIONS_STRATEGY).navigation();
            }
        });

        /*定投*/
        ClickUtil.click(llDingTou, () -> {
            if (checkCetification()) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.functions_functionsFragment2));
            }
        });

        /*跑得快*/
        ClickUtil.click(300, llPaodeKuai, () -> {
            if (checkCetification()) {
                ARouter.getInstance().build(RConfig.BOOM_ACTIVITY_BOOM).navigation();
            }
        });

        /*大转盘*/
        ClickUtil.click(300, llZhuanPan, () -> {
            if (checkCetification()) {
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.append(BaseHttpConfig.ZHUANPAN);
                stringBuilder.append("?token=");
                stringBuilder.append(SPUtil.get(SPConfig.TOKEN, ""));
                stringBuilder.append("?account=");
                stringBuilder.append(SPUtil.get(SPConfig.ACCOUNT, ""));
                stringBuilder.append("?lang=");

                if (Constans.LANGUAGE_EN.equals(SpApplication.getLANGUAGE())) {
                    stringBuilder.append("EN");
                } else if (Constans.LANGUAGE_CNA.equals(SpApplication.getLANGUAGE())) {
                    stringBuilder.append("CN");
                } else if (Constans.LANGUAGE_TRA.equals(SpApplication.getLANGUAGE())) {
                    stringBuilder.append("TW");
                }
                ARouter.getInstance().build(RConfig.APP_ACTIVITY_ZHUANPAN)
                        .withString(Constans.TITLE, App.INSTANCE.getString(R.string.functions_functionsFragment3))
                        .withString(Constans.WEB_URL, stringBuilder.toString())
                        .navigation();
            }
        });

        ClickUtil.click(300, functionsSwhzRl, () -> {
            if (checkCetification()) {
                ARouter.getInstance().build(RConfig.APP_ACTIVITY_ZHUANPAN)
                        .withString(Constans.TITLE, App.INSTANCE.getString(R.string.functions_functionsFragment4))
                        .withString(Constans.WEB_URL, BaseHttpConfig.SHANGWU)
//                        .withString(Constans.WEB_URL, BaseHttpConfig.HETONG)
                        .navigation();
            }
        });
    }

    @SuppressLint("CheckResult")
    public void getBannerSuccess(List<FunctionsBannerBean> data) {
        if (data == null || data.size() == 0) {
            return;
        }
        if (banner == null) {
            return;
        }
        Observable.fromArray(data)
                .map(dataBean -> {
                    List<String> imgUrls = new ArrayList<>();
                    for (int i = 0; i < dataBean.size(); i++) {
                        imgUrls.add(BaseHttpConfig.BASE_URL + dataBean.get(i).getPath());
                    }
                    return imgUrls;
                }).compose(RxSchedulersHelper.io_main())
                .subscribe(strings -> {
                    banner.setImages(strings);
                    banner.start();
                    banner.setOnBannerListener(position -> ARouter.getInstance().build(RConfig.APP_ACTIVITY_ZHUANPAN)
                            .withString(Constans.TITLE, data.get(position).getName())
                            .withString(Constans.WEB_URL, data.get(position).getUrl())
                            .navigation());
                });
    }

    public void getStatusOK(CertificationBean data) {

    }
}
